package com.starmark.gateway.springcloud.provider.swagger;

import cn.hutool.core.util.StrUtil;
import com.starmark.gateway.springcloud.provider.constants.GatewayConstant;
import com.starmark.gateway.springcloud.provider.utils.RequestHelper;

import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;

/**
 * @author starmark
 * @date 20-3-8  下午1:32
 */
@Component
public class SwaggerFilter implements WebFilter {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
        //获取工程编码
        String projectCode= RequestHelper.getRequestProjectCode(exchange.getRequest());
        if (StrUtil.isEmpty(projectCode)) {
            if (exchange.getRequest().getURI().getPath().contains(projectCode + "/doc.html")) {
                return chain.filter(exchange.mutate().request(exchange.getRequest().mutate().path("/doc.html").build()).build());

            } else if (exchange.getRequest().getURI().getPath().contains(projectCode + "/webjars/")) {
                return chain.filter(exchange.mutate().request(exchange.getRequest().mutate().path(exchange.getRequest().getURI().getPath().replace("/" + projectCode, "")).build()).build());
            }
        }


        return chain.filter(exchange);
    }

}
